﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using RiskMan.DataClass;
using RiskMan.DataClass.Models;
using System.Collections.ObjectModel;

namespace RiskMan.Views
{
    class VerificationLicense
    {
        private static List<string> countAccountLicense = new List<string>();
        private static List<string> countClientcodeLicense = new List<string>();
        private static List<string> tmpcountAccountLicense;
        private static List<string> tmpcountClientcodeLicense;

        /// <summary>
        /// Добавление счетов в лист, при запуске счета в модуле
        /// </summary>
        /// <param name="mainWindow"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CountAccountLicense(MainWindow mainWindow, string model)
        {
            tmpcountAccountLicense = new List<string>();
            tmpcountClientcodeLicense = new List<string>();
            //проверка счетов по РМ
            if (model == "RM")
                for (int i = 0; i < mainWindow._dataProvider.RMItems.Count; i++)
                {
                    //подлкючаемые счета и включенные терминалы
                    if ((mainWindow._dataProvider.RMItems[i].Check == true && mainWindow._dataProvider.RMItems[i].isRun == false) || mainWindow._dataProvider.RMItems[i].isRun == true)
                    {
                        if (mainWindow._dataProvider.RMItems[i].Account != mainWindow._dataProvider.RMItems[i].ClientCode)
                        {
                            if (countAccountLicense.Contains(mainWindow._dataProvider.RMItems[i].ClientCode) == false && tmpcountClientcodeLicense.Contains(mainWindow._dataProvider.RMItems[i].ClientCode) == false)
                                tmpcountAccountLicense.Add(mainWindow._dataProvider.RMItems[i].ClientCode);
                        }
                        else
                            if (countClientcodeLicense.Contains(mainWindow._dataProvider.RMItems[i].ClientCode) == false && tmpcountClientcodeLicense.Contains(mainWindow._dataProvider.RMItems[i].ClientCode) == false)
                                tmpcountClientcodeLicense.Add(mainWindow._dataProvider.RMItems[i].ClientCode);
                    }
                }

            //проверка счетов по стоп-лосс
            if (model == "SL")
                for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
                {
                    if ((mainWindow._dataProvider.StopList[i].Check == true && mainWindow._dataProvider.StopList[i].IsStart == false) || mainWindow._dataProvider.StopList[i].IsStart == true)
                    {
                        if (mainWindow._dataProvider.StopList[i].Account != mainWindow._dataProvider.StopList[i].ClientCode)
                        {
                            if (countAccountLicense.Contains(mainWindow._dataProvider.StopList[i].ClientCode) == false && tmpcountAccountLicense.Contains(mainWindow._dataProvider.StopList[i].ClientCode) == false)
                                tmpcountAccountLicense.Add(mainWindow._dataProvider.StopList[i].ClientCode);
                        }
                        else
                            if (countClientcodeLicense.Contains(mainWindow._dataProvider.StopList[i].ClientCode) == false && tmpcountClientcodeLicense.Contains(mainWindow._dataProvider.StopList[i].ClientCode)==false)
                                tmpcountClientcodeLicense.Add(mainWindow._dataProvider.StopList[i].ClientCode);
                    }
                }

            //проверка счетов по РМ
            if (model == "RMinvestor")
                for (int i = 0; i < mainWindow._dataProvider.RmInvestorItems.Count; i++)
                {
                    //подлкючаемые счета и включенные терминалы
                    if ((mainWindow._dataProvider.RmInvestorItems[i].Check == true && mainWindow._dataProvider.RmInvestorItems[i].Run == false) || mainWindow._dataProvider.RmInvestorItems[i].Run == true)
                    {
                        if (mainWindow._dataProvider.RmInvestorItems[i].Account != mainWindow._dataProvider.RmInvestorItems[i].ClientCode)
                        {
                            if (countAccountLicense.Contains(mainWindow._dataProvider.RmInvestorItems[i].ClientCode) == false && tmpcountClientcodeLicense.Contains(mainWindow._dataProvider.RmInvestorItems[i].ClientCode) == false)
                                tmpcountAccountLicense.Add(mainWindow._dataProvider.RmInvestorItems[i].ClientCode);
                        }
                        else
                            if (countClientcodeLicense.Contains(mainWindow._dataProvider.RmInvestorItems[i].ClientCode) == false && tmpcountClientcodeLicense.Contains(mainWindow._dataProvider.RmInvestorItems[i].ClientCode) == false)
                                tmpcountClientcodeLicense.Add(mainWindow._dataProvider.RmInvestorItems[i].ClientCode);
                    }
                }

            if ((tmpcountAccountLicense.Count + countAccountLicense.Count) <= MainWindow.countLisence && (tmpcountClientcodeLicense.Count+countClientcodeLicense.Count) <= MainWindow.countLisence)
            {
                countAccountLicense.AddRange(tmpcountAccountLicense);
                countClientcodeLicense.AddRange(tmpcountClientcodeLicense);
                return true;
            }
            else
                return false;
        }



        /// <summary>
        /// Функция удаления счетов из листа
        /// </summary>
        /// <param name="mainWindow"></param>
        /// <param name="model"></param>
        public void RemoveAccountLicense(MainWindow mainWindow, string model)
        {
            //проверка счетов по РМ
            if (model == "RM")
                for (int i = 0; i < mainWindow._dataProvider.RMItems.Count; i++)
                {
                    if (mainWindow._dataProvider.RMItems[i].Check == true && mainWindow._dataProvider.RMItems[i].isRun == true)
                        test(mainWindow, "RM", mainWindow._dataProvider.RMItems[i].ClientCode);
                }

            //проверка счетов по стоп-лосс
            if (model == "SL")
                for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
                {
                    if (mainWindow._dataProvider.StopList[i].Check == true && mainWindow._dataProvider.StopList[i].IsStart == true)
                        test(mainWindow, "SL", mainWindow._dataProvider.StopList[i].ClientCode);
                }

            //проверка счетов по РМ
            if (model == "RMinvestor")
                for (int i = 0; i < mainWindow._dataProvider.RmInvestorItems.Count; i++)
                {
                    if (mainWindow._dataProvider.RmInvestorItems[i].Check == true && mainWindow._dataProvider.RmInvestorItems[i].Run == true)
                        test(mainWindow, "RMinvestor", mainWindow._dataProvider.RmInvestorItems[i].ClientCode);
                }
        }

 
        /// <summary>
        /// Проверка на наличие запущенных счетов в других модулях
        /// </summary>
        /// <param name="mainWindow"></param>
        /// <param name="model"></param>
        /// <param name="clientcode"></param>
        /// <returns></returns>
        private void test(MainWindow mainWindow, string model, string clientcode)
        {
            bool flag = false;

            if (model == "RM")
            {
                for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
                    if (mainWindow._dataProvider.StopList[i].IsStart == true && mainWindow._dataProvider.StopList[i].ClientCode == clientcode)
                        flag = true;

                for (int i = 0; i < mainWindow._dataProvider.RmInvestorItems.Count; i++)
                    if (mainWindow._dataProvider.RmInvestorItems[i].Run == true && mainWindow._dataProvider.RmInvestorItems[i].ClientCode == clientcode)
                        flag = true;

            }

            if (model == "RMinvestor")
            {
                for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
                    if (mainWindow._dataProvider.StopList[i].IsStart == true && mainWindow._dataProvider.StopList[i].ClientCode == clientcode)
                        flag = true;

                for (int i = 0; i < mainWindow._dataProvider.RMItems.Count; i++)
                    if (mainWindow._dataProvider.RMItems[i].isRun == true && mainWindow._dataProvider.RMItems[i].ClientCode == clientcode)
                        flag = true;

            }

            if (model == "SL")
            {
                for (int i = 0; i < mainWindow._dataProvider.RMItems.Count; i++)
                    if (mainWindow._dataProvider.RMItems[i].isRun == true && mainWindow._dataProvider.RMItems[i].ClientCode == clientcode)
                        flag = true;

                for (int i = 0; i < mainWindow._dataProvider.RmInvestorItems.Count; i++)
                    if (mainWindow._dataProvider.RmInvestorItems[i].Run == true && mainWindow._dataProvider.RmInvestorItems[i].ClientCode == clientcode)
                        flag = true;

            }


            if (flag == false)
            {
                if (countAccountLicense.Contains(clientcode) == true)
                    countAccountLicense.Remove(clientcode);

                if (countClientcodeLicense.Contains(clientcode) == true)
                    countClientcodeLicense.Remove(clientcode);

            }
        }

    }
}
